package com.github.icloudpay.crm.core.biz;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.github.icloudpay.crm.core.client.FlyrayBizService;
import com.github.icloudpay.crm.core.entity.PersonalAccount;
import com.github.icloudpay.crm.core.entity.PersonalBase;
import com.github.icloudpay.crm.core.entity.PersonalBaseExt;
import com.github.icloudpay.crm.core.mapper.PersonalAccountMapper;
import com.github.icloudpay.crm.core.mapper.PersonalBaseExtMapper;
import com.github.icloudpay.crm.core.mapper.PersonalBaseMapper;
import com.github.wxiaoqi.security.common.biz.BaseBiz;
import com.github.wxiaoqi.security.common.msg.AccountType;
import com.github.wxiaoqi.security.common.msg.ResponseCode;

/**
 * 个人基础信息
 *
 * @author centerroot
 * @email lfw6699@163.com
 * @date 2018-05-25 15:20:48
 */
@Service
public class PersonalBaseBiz extends BaseBiz<PersonalBaseMapper,PersonalBase> {
	private static final Logger logger = LoggerFactory.getLogger(PersonalBaseBiz.class);
	
	@Autowired
	private PersonalBaseMapper personalBaseMapper;
	@Autowired
	private PersonalBaseExtMapper personalBaseExtMapper;
	@Autowired
	private PersonalAccountMapper personalAccountMapper;
	@Autowired
	private PersonalAccountJournalBiz personalAccountJournalBiz;
	@Autowired
	private SmsGateWayBiz smsGateWayBiz;
	@Autowired
	private FlyrayBizService flyrayBizService;
	
	@Value("${bountyHunter.acceptBizNo}")
	private String acceptBizNo;
	
	/**
	 * 获取猎手中心统计数据
	 * @author centerroot
	 * @time 创建时间:2018年5月18日上午10:37:58
	 * @return
	 */
	public Map<String, Object> getCustStatistics(Map<String, Object> param){
		logger.info("获取猎手中心统计数据 请求参数：{}",param);
		Map<String, Object> respMap = new HashMap<String, Object>();
		String perId = (String) param.get("perId");
		PersonalBase personalBase = personalBaseMapper.selectByPrimaryKey(perId);
		if (null != personalBase) {
			// 查询用户账户可用赏金
			PersonalAccount reqCustAcc = new PersonalAccount();
			reqCustAcc.setPerId(personalBase.getPerId());
			reqCustAcc.setAccType(AccountType.ACC_BALANCE.getCode());// 余额账户
			PersonalAccount respCustAcc = personalAccountMapper.selectOne(reqCustAcc);
			if (null != respCustAcc) {
				respMap.put("accBalance", respCustAcc.getAccBalance());
			}
			
			// 查询用户昨日收到赏金
			Map<String, Object> yestMap = personalAccountJournalBiz.getYesterdayIncome(personalBase);
			if (null != yestMap && ResponseCode.OK.getCode().equals(yestMap.get("code"))) {
				respMap.put("yesterdayIncome", yestMap.get("yesterdayIncome"));
			}else{
				respMap.put("yesterdayIncome", "0.00");
			}
			
			
			PersonalBaseExt personalBaseExt = personalBaseExtMapper.selectByPrimaryKey(perId);
			if (null != personalBaseExt) {
				//  查询用户关注的行业匹配悬赏、关注的职位匹配悬赏、我的标签匹配悬赏、昨日被查看简历数、昨日已淘汰简历数
				Map<String, Object> reqHunterMap = new HashMap<String, Object>();
				reqHunterMap.put("attentionIndustry", personalBaseExt.getAttentionIndustry());
				reqHunterMap.put("companyFunction", personalBaseExt.getAttentionJobs());
				reqHunterMap.put("selfTag", personalBaseExt.getSelfTag());
				reqHunterMap.put("custNo", perId);
				Map<String, Object> hunterMap = flyrayBizService.getHunterCenterInfo(reqHunterMap);
				if (null != hunterMap && ResponseCode.OK.getCode().equals(hunterMap.get("code"))) {
					// 关注的行业匹配悬赏
					respMap.put("industryCount", hunterMap.get("industryCount"));
					// 关注的职位匹配悬赏
					respMap.put("functionCount", hunterMap.get("functionCount"));
					// 我的标签匹配悬赏
					respMap.put("tagCount", hunterMap.get("tagCount"));
					// 昨日被查看简历数
					respMap.put("viewCount", hunterMap.get("viewCount"));
					// 昨日已淘汰简历数
					respMap.put("failedCount", hunterMap.get("failedCount"));
				} else {
					respMap.put("industryCount", "0");
					respMap.put("functionCount", "0");
					respMap.put("tagCount", "0");
					respMap.put("viewCount", "0");
					respMap.put("failedCount", "0");
				}
			} else {
				respMap.put("industryCount", "0");
				respMap.put("functionCount", "0");
				respMap.put("tagCount", "0");
				respMap.put("viewCount", "0");
				respMap.put("failedCount", "0");
			}
			
			respMap.put("code", ResponseCode.OK.getCode());
			respMap.put("msg", ResponseCode.OK.getMessage());
		}else{
			respMap.put("code", ResponseCode.CUST_NOTEXIST.getCode());
			respMap.put("msg", ResponseCode.CUST_NOTEXIST.getMessage());
		}

		logger.info("获取猎手中心统计数据 响应参数：{}",respMap);
		return respMap;
	}
	
	/**
	 * 保存用户基础信息
	 * @author centerroot
	 * @time 创建时间:2018年5月21日上午10:53:24
	 * @param param
	 * @return
	 */
	public Map<String, Object> saveCustBaseInfo(Map<String, Object> param){
		logger.info("保存用户基础信息 请求参数：{}",param);
		Map<String, Object> respMap = new HashMap<String, Object>();
		String perId = (String) param.get("perId");
		try {
			PersonalBase personalBase = personalBaseMapper.selectByPrimaryKey(perId);
			personalBase.setNickName((String) param.get("nickName"));
			if ("00".equals(personalBase.getVerifyFlag())) {
				personalBase.setRealName((String) param.get("realName"));
			}
			personalBase.setSex((String) param.get("sex"));
			personalBase.setBirthday((String) param.get("birthday"));
			personalBase.setAddress((String) param.get("address"));
			personalBase.setHometown((String) param.get("hometown"));
			personalBaseMapper.updateByPrimaryKey(personalBase);
			
			respMap.put("code", ResponseCode.OK.getCode());
			respMap.put("msg", ResponseCode.OK.getMessage());
		}catch(Exception e){
			e.printStackTrace();
			respMap.put("code", ResponseCode.ABNORMAL_FIELDS.getCode());
			respMap.put("msg", ResponseCode.ABNORMAL_FIELDS.getMessage());
		}
		
		
		logger.info("保存用户基础信息 响应参数：{}",respMap);
		return respMap;
	}
	
	/**
	 * 用户安全信息实名认证
	 * @author centerroot
	 * @time 创建时间:2018年5月22日下午2:21:01
	 * @param param
	 * @return
	 */
	public Map<String, Object> verifiedRealName(Map<String, Object> param){
		logger.info("用户安全信息实名认证   请求参数：{}",param);
		Map<String, Object> respMap = new HashMap<String, Object>();
		String perId = (String) param.get("perId");
		String realName = (String) param.get("realName");
		String idNumber = (String) param.get("idNumber");
		String mobile = (String) param.get("mobile");
		String idPositiveStr = (String) param.get("idPositiveStr");
		String idNegativeStr = (String) param.get("idNegativeStr");
		PersonalBase personalBase = personalBaseMapper.selectByPrimaryKey(perId);
		if (null != personalBase) {
			personalBase.setRealName(realName);
			personalBase.setIdNumber(idNumber);
			personalBase.setIdNegativeStr(idNegativeStr);
			personalBase.setIdPositiveStr(idPositiveStr);
			personalBase.setVerifyFlag("02"); // 待审核
			personalBaseMapper.updateByPrimaryKey(personalBase);
			
			respMap.put("code", ResponseCode.OK.getCode());
			respMap.put("msg", ResponseCode.OK.getMessage());
			
		}else{
			respMap.put("code", ResponseCode.CUST_NOTEXIST.getCode());
			respMap.put("msg", ResponseCode.CUST_NOTEXIST.getMessage());
		}
		
		
		logger.info("用户安全信息实名认证   响应参数：{}",respMap);
		return respMap;
	}
	
	
}